//
//  LXLMeCollectionViewModel.m
//  budejie
//
//  Created by 朱建鹏 on 16/8/25.
//  Copyright © 2016年 Dason. All rights reserved.
//

#import "LXLMeCollectionViewModel.h"

@interface LXLMeCollectionViewModel ()



@end

@implementation LXLMeCollectionViewModel

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{};

- (NSString *)description{
    return [NSString stringWithFormat:@"%@  %@  %@",_name,_icon,_url];
}

+ (instancetype)LXLMeCollectionViewModelWithDic:(NSDictionary *)dic{
    LXLMeCollectionViewModel *model = [LXLMeCollectionViewModel new];

    
    [model setValuesForKeysWithDictionary:dic];
    
    return model;
}

+ (void)LXLMeCollectionViewModelDataWithURL:(NSURL *)url andKey:(NSString *)key succeedBlock:(void(^)(NSArray *))succeedBlock failureBlock:(void(^)(NSError *))failureBlock{
    
   

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
    
        if(connectionError == nil && data.length > 0){
            
          NSMutableArray *arrM = [NSMutableArray new];
            
            NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            
            NSArray *arr = dic[key];
            
            [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
               
                [arrM addObject:[LXLMeCollectionViewModel LXLMeCollectionViewModelWithDic:obj]];
            }];
            
            if(succeedBlock){
                succeedBlock(arrM.copy);
            }
        }else{
        
            if(failureBlock){
                
                failureBlock(connectionError);
            }
        }
        
    }];
    
    

}




@end
